<?php
namespace frontend\modules\yii\widgets;

use frontend\modules\yii\assets\markdown\MarkdownAsset;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;

/**
 * A menu imitate the sidebar of https://docs.docker.com/get-started/
 *
 * @note It's depend on bootstrap;
 *
 * @example :
 * ```php

 * ```
 * @author chenzhiwei <chenzhiweiwk@qq.com>
 * @date   2021/08/31
 */
class Markdown extends Widget
{
    public $upload;
    public $baseUrl;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->baseUrl = MarkdownAsset::register($this->getView())->baseUrl;
        $this->initMarkdown();
    }

    public function run()
    {
        echo "<div id='editor'></div>";
    }

    public function initMarkdown(){
        $js = <<<JS
    var editor = editormd("editor", {
        mode                 : "gfm", 
        width: "100%",
        height: "100%",
        imageUpload: true,
        imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: '{$this->upload}',
        flowchart: true,
        syncScrolling : true,
        // markdown: "xxxx",     // dynamic set Markdown text
        path : "{$this->baseUrl}/lib/"  // Autoload modules mode, codemirror, marked... dependents libs path
    });
JS;
        \Yii::$app->view->registerJs($js, View::POS_READY);

    }
}
